################################
###                          ###
### Women and Party Building ###
###                          ###
###     code_analysis.R      ###
###                          ###
################################

# This script performs the analyses for the main text of the article.

rm( list=ls() )
library(rdrobust)
library(plotrix)
library(stargazer)

cat("Beginning main analyses...\n")

# This line uses the rstudioapi package to set the working directory to the same folder where this script is saved.
# Alternatively, you can use setwd( PATH ) to set this directory manually
try(setwd(dirname(rstudioapi::getActiveDocumentContext()$path)))

# Loads the helper functions into memory
source("code_helperfunctions.R")

# Creates sub-folders in the working directory for saving the results
dir.create("../3_output/", showWarnings = FALSE)
dir.create("../3_output/figures", showWarnings = FALSE)
dir.create("../3_output/tables", showWarnings = FALSE)
dir.create("../3_output/appendix", showWarnings = FALSE)



### FIGURE 1: Party Membership Recruitment of Women in Brazil by Party, 2005-2020

gender.members <- readRDS("../1_data/analysis/data_analysis_MemberBalance.rds")

pdf("../3_output/figures/figure1.pdf", width=6.5, height=4.5)
par(las=1, mar= c(2.1, 4.1, 2.1, 0.1))
barplot(gender.members, col=c("red3", "deepskyblue1"), names.arg = colnames(gender.members), ylab="Share of Party Member Recruits", legend.text = c("Women", "Men"), cex.names=.8)
abline(h=.508, lty=2, col="black", lwd=4)
dev.off()




### TABLE 1. Rates of Party Switching Among Brazilian Party Members by Gender, 2005-2020

switch.data <- readRDS("../1_data/analysis/data_analysis_PartySwitchingByGender.rds")

stargazer(switch.data, summary=F, out="../3_output/tables/table1.tex",
          title="Rates of Party Switching Among Brazilian Party Members by Gender, 2005-2020", label="tab:Party_Switching")




### FIGURE 2. Gender Gap in Party Membership across Brazil’s municipal constituencies, 2005-2020
### (Distribution of Main Dependent Variable)

women <- readRDS("../1_data/analysis/data_analysis_RDD_women.rds")
men <- readRDS("../1_data/analysis/data_analysis_RDD_men.rds")


pdf("../3_output/figures/figure2.pdf", width=6, height=4)
par(las=1, mar=c(5,5,1,1))
dv <- women$Gender.Gap[!is.na(women$Gender.Gap) & women$Gender.Gap != 0]
den <- density(dv)
plot(den, xlim=c(-2, 8), xlab="Gender Gap in Membership Recruitment", main="", ylim=c(0, .55))
abline(v=0, lty=3, lwd=1)
y <- .5
text.size <- .8
text(0, y, "Biased\nTowards\nMen", pos=4, cex=text.size)
text(0, y, "Biased\nTowards\nWomen", pos=2, cex=text.size)
value <- 0
polygon(den, col = "gray80", border = 1)
polygon(c(den$x[den$x >= value ], value),
        c(den$y[den$x >= value ], 0),
        col = "gray30",
        border = 1)
dev.off()

# Calculate the average gender gap for in-text discussion
mean(women$Gender.Gap, na.rm=T)

# Calculate the share of municipalities in which recruitment of men exceeded 
# recruitment of women, among the subset of municipalities that had any recruits
mean(women$Gender.Gap[women$Recruit.Share > 0] > 0, na.rm=T)



### FIGURE 3. The Effect of Female Mayors on Membership Recruitment into the Female Candidate’s Party

women <- readRDS("../1_data/analysis/data_analysis_RDD_women.rds")
men <- readRDS("../1_data/analysis/data_analysis_RDD_men.rds")

pdf("../3_output/figures/figure3.pdf", width=8, height=4.5)
par(mfrow=c(1,2), las=1)
rdplot(y=women$Recruit.Share, x=women$Margin,  x.label = "Female Candidate's Margin of Victory", y.label = "New Members per 1,000 Voters", y.lim=c(0,10), x.lim=c(-0.1,0.1), nbins = 100, title="(A) Recruitment Rate", cex.main = .9, p=5)
rdplot(y=women$Gender.Gap, x=women$Margin,  x.label = "Female Candidate's Margin of Victory", y.label = "Gender Gap of Recruitment", y.lim=c(0,2), x.lim=c(-0.1,0.1), nbins = 100, title="(B) Gender Gap in Recruitment", cex.main = .9, p=5)
dev.off()



### FIGURE 4. The Effect of Incumbency on Membership Recruitment, by Winner’s Gender

women <- readRDS("../1_data/analysis/data_analysis_RDD_women.rds")
men <- readRDS("../1_data/analysis/data_analysis_RDD_men.rds")

mod.recruitment.women <- rdrobust(y=women$Recruit.Share, x=women$Margin)
mod.recruitment.men <- rdrobust(y=men$Recruit.Share, x=men$Margin)
mod.imbalance.women <- rdrobust(y=women$Gender.Gap, x=women$Margin)
mod.imbalance.men <- rdrobust(y=men$Gender.Gap, x=men$Margin)
coefs <- c(mod.recruitment.women$coef[3], mod.recruitment.men$coef[3], mod.imbalance.women$coef[3], mod.imbalance.men$coef[3])
confs <- rbind(mod.recruitment.women$ci[3,], 
               mod.recruitment.men$ci[3,], 
               mod.imbalance.women$ci[3,], 
               mod.imbalance.men$ci[3,])

pdf("../3_output/figures/figure4.pdf", width=8, height=3.5)
par(mfrow=c(1,2), las=1, mar=c(2, 4, 2, 2))
plotCI(1:2, coefs[1:2], ui=confs[1:2,2], li=confs[1:2,1], pch=16, lwd=2, cex=1, xlim=c(.5, 2.5), ylim=c(-3, 2), xaxt="n", xlab="", ylab="New Members per 1,000 Voters", main="(A) Recruitment Rate", cex.main=.9)
abline(h=0, lty=3)
axis(1, 1:2, labels = c("Female\nMayor", "Male\nMayor"), tick = F)
plotCI(1:2, coefs[3:4], ui=confs[3:4,2], li=confs[3:4,1], pch=16, lwd=2, cex=1, xlim=c(.5, 2.5), ylim=c(-2, 1), xaxt="n", xlab="", ylab="Gender Gap in Recruitment", main="(B) Gender Gap in Recruitment", cex.main=.9)
abline(h=0, lty=3)
axis(1, 1:2, labels = c("Female\nMayor", "Male\nMayor"), tick = F)
dev.off()






### FIGURE 5. The Effect of Incumbency on Membership Attrition, by gender of mayor and members

women <- readRDS("../1_data/analysis/data_analysis_RDD_women.rds")
men <- readRDS("../1_data/analysis/data_analysis_RDD_men.rds")

mod.attrition.female <- rdrobust(y=women$Attrition.Rate, x=women$Margin)
mod.attrition.male <- rdrobust(y=men$Attrition.Rate, x=men$Margin)
mod.attrition.women.female <- rdrobust(y=women$Attrition.Rate.Female, x=women$Margin)
mod.attrition.women.male <- rdrobust(y=men$Attrition.Rate.Female, x=men$Margin)
mod.attrition.men.female <- rdrobust(y=women$Attrition.Rate.Male, x=women$Margin)
mod.attrition.men.male <- rdrobust(y=men$Attrition.Rate.Male, x=men$Margin)

coefs <- c(mod.attrition.female$coef[3], 
           mod.attrition.male$coef[3], 
           mod.attrition.women.female$coef[3],
           mod.attrition.women.male$coef[3], 
           mod.attrition.men.female$coef[3], 
           mod.attrition.men.male$coef[3])
confs <- rbind(mod.attrition.female$ci[3,], 
               mod.attrition.male$ci[3,],
               mod.attrition.women.female$ci[3,],
               mod.attrition.women.male$ci[3,],
               mod.attrition.men.female$ci[3,],
               mod.attrition.men.male$ci[3,])

pdf("../3_output/figures/figure5.pdf", width=6, height=4.5)
par(las=1)
gap <- .1
places <- c(1-gap, 1+gap, 2-gap, 2+gap, 3-gap, 3+gap)
plotCI(places, coefs, ui=confs[,2], li=confs[,1], pch=16, lwd=2, cex=1, xlim=c(.5, 3.5), ylim=c(-.1, .12), xaxt="n", ylab="Effect on Membership Attrition", xlab="", col=c("black", "gray50"))
abline(h=0, lty=3)
legend("topright", lwd=2, pch=c(16,21), col=c("black", "gray50"), pt.bg = c("black", "white"), c("Female Mayor", "Male Mayor"), bty="n")
axis(1, 1:3, labels = c("All\nMembers", "Female\nMembers", "Male\nMembers"), tick = F)
segments(places, confs[,1], places, confs[,2], lwd=1, col=c("black", "gray50"))
points(places, coefs, pch=c(16, 21), cex=1.5, col=c("black", "gray50"), bg=c("black", "white"))
dev.off()






### FIGURE 6. The Effect of the Election Outcome on the Candidate’s Career Trajectory

women <- readRDS("../1_data/analysis/data_analysis_RDD_women.rds")
men <- readRDS("../1_data/analysis/data_analysis_RDD_men.rds")

renominated.women <- rdrobust(y=women$Renominated[!women$Incumbent], x=women$Margin[!women$Incumbent])
renominated.men <- rdrobust(y=men$Renominated[!men$Incumbent], x=men$Margin[!men$Incumbent])
advanced.women <- rdrobust(y=women$Advanced, x=women$Margin)
advanced.men <- rdrobust(y=men$Advanced, x=men$Margin)

renominated <- difference.calculator(renominated.women, renominated.men)
advanced <- difference.calculator(advanced.women, advanced.men)

pdf("../3_output/figures/figure6.pdf", width=7, height=3.5)
which <- 1:2
par(las=1, mar=c(2,4,2,2), mfrow=c(1,2))
plotCI(which, renominated[which, 1], ui=renominated[which, 3], li=renominated[which, 2], pch=16, lwd=2, cex=1, xlim=c(.5, 2.5), ylim=c(-.3, .3), xaxt="n", xlab="", ylab="Effect on Chances of Renomination", main="(A) Renomination as Mayor", cex.main=.9)
abline(h=0, lty=3)
axis(1, 1:2, labels = c("Female\nCandidates", "Male\nCandidates"), tick = F)
plotCI(which, advanced[which, 1], ui=advanced[which, 3], li=advanced[which, 2], pch=16, lwd=2, cex=1, xlim=c(.5, 2.5), ylim=c(-.3, .3), xaxt="n", xlab="", ylab="Effect on Chances of Advancement", main="(B) Nomination to State/Federal Office", cex.main=.9)
abline(h=0, lty=3)
axis(1, 1:2, labels = c("Female\nCandidates", "Male\nCandidates"), tick = F)
dev.off()






### FIGURE 7. The Effect of the Election Outcome on Nomination for Higher Office, by Women’s Party Membership in Previous Terms

women <- readRDS("../1_data/analysis/data_analysis_RDD_women.rds")
men <- readRDS("../1_data/analysis/data_analysis_RDD_men.rds")

# Nomination to higher office
# (We are partitioning the samples at the median value of cumulative women's recruitment, and we are calculating the median separately for each term)
median.membership.04 <- median(women$Cumulative.Female.Recruits.Share[women$Year == 2004 & !is.na(women$Advanced)], na.rm=T)
median.membership.08 <- median(women$Cumulative.Female.Recruits.Share[women$Year == 2008 & !is.na(women$Advanced)], na.rm=T)
median.membership.12 <- median(women$Cumulative.Female.Recruits.Share[women$Year == 2012 & !is.na(women$Advanced)], na.rm=T)
median.membership.16 <- median(women$Cumulative.Female.Recruits.Share[women$Year == 2016 & !is.na(women$Advanced)], na.rm=T)
sub <- (women$Year == 2004 & women$Cumulative.Female.Recruits.Share > median.membership.04) | 
  (women$Year == 2008 & women$Cumulative.Female.Recruits.Share > median.membership.08) |
  (women$Year == 2012 & women$Cumulative.Female.Recruits.Share > median.membership.12) |
  (women$Year == 2016 & women$Cumulative.Female.Recruits.Share > median.membership.16)

advanced.women.highfemalemembership <- rdrobust(y=women$Advanced[sub], x=women$Margin[sub])
advanced.women.lowfemalemembership <- rdrobust(y=women$Advanced[!sub], x=women$Margin[!sub])
advanced.men.highfemalemembership <- rdrobust(y=men$Advanced[sub], x=men$Margin[sub])
advanced.men.lowfemalemembership <- rdrobust(y=men$Advanced[!sub], x=men$Margin[!sub])


# Gender gap in recruitment
median.membership.04 <- median(women$Cumulative.Female.Recruits.Share[women$Year == 2004 & !is.na(women$Gender.Gap)], na.rm=T)
median.membership.08 <- median(women$Cumulative.Female.Recruits.Share[women$Year == 2008 & !is.na(women$Gender.Gap)], na.rm=T)
median.membership.12 <- median(women$Cumulative.Female.Recruits.Share[women$Year == 2012 & !is.na(women$Gender.Gap)], na.rm=T)
median.membership.16 <- median(women$Cumulative.Female.Recruits.Share[women$Year == 2016 & !is.na(women$Gender.Gap)], na.rm=T)
sub <- (women$Year == 2004 & women$Cumulative.Female.Recruits.Share > median.membership.04) | 
  (women$Year == 2008 & women$Cumulative.Female.Recruits.Share > median.membership.08) |
  (women$Year == 2012 & women$Cumulative.Female.Recruits.Share > median.membership.12) |
  (women$Year == 2016 & women$Cumulative.Female.Recruits.Share > median.membership.16)

gender.imbalance.FEMALEmayor.HIGHpastrecruitment <- rdrobust(y=women$Gender.Gap[sub], x=women$Margin[sub])
gender.imbalance.FEMALEmayor.LOWpastrecruitment <- rdrobust(y=women$Gender.Gap[!sub], x=women$Margin[!sub])
gender.imbalance.MALEmayor.HIGHpastrecruitment <- rdrobust(y=men$Gender.Gap[sub], x=men$Margin[sub])
gender.imbalance.MALEmayor.LOWpastrecruitment <- rdrobust(y=men$Gender.Gap[!sub], x=men$Margin[!sub])

advanced.highlowmembership <- rbind(difference.calculator(advanced.women.highfemalemembership, advanced.men.highfemalemembership), difference.calculator(advanced.women.lowfemalemembership, advanced.men.lowfemalemembership))
gap.highlowmembership <- rbind(difference.calculator(gender.imbalance.FEMALEmayor.HIGHpastrecruitment, gender.imbalance.MALEmayor.HIGHpastrecruitment), difference.calculator(gender.imbalance.FEMALEmayor.LOWpastrecruitment, gender.imbalance.MALEmayor.LOWpastrecruitment))



pdf("../3_output/figures/figure7.pdf", width=8, height=3.5)
gap <- .1
places <- sort(c(1, 1, 2, 2) + c(-gap, gap))
par(las=1, mar=c(2,4,2,2), mfrow=c(1,2))
which <- c(1,2, 4, 5)
plotCI(places, advanced.highlowmembership[which, 1], ui=advanced.highlowmembership[which, 3], li=advanced.highlowmembership[which, 2], pch=16, lwd=2, cex=1.3, xlim=c(.5, 2.5), ylim=c(-.5, .6), xaxt="n", xlab="", ylab="Effect on Chances of Advancement", main="(A) Nomination to Higher Office", cex.main=.9, col=c("black", "gray50"))
axis(1, 1:2, labels = c("High\n", "Low\n"), tick = F)
axis(1, 1.5, labels = "\nRecruitment of Women in Previous Terms", tick = F)
abline(h=0, lty=3)
points(places, advanced.highlowmembership[which, 1], pch=c(16, 21), lwd=2, cex=1.3, bg=c("black", "white"), col=c("black", "gray50"))
legend("topright", col=c("black", "gray50"), pch=c(16, 21), pt.bg=c("black", "white"), c("Female Mayors", "Male Mayors"), lwd=2, bty="n")

which <- 1:2
gap <- .1
places <- sort(c(1, 1, 2, 2) + c(-gap, gap))
which <- c(1,2, 4, 5)
plotCI(places, gap.highlowmembership[which, 1], ui=gap.highlowmembership[which, 3], li=gap.highlowmembership[which, 2], pch=16, lwd=2, cex=1.3, xlim=c(.5, 2.5), ylim=c(-3, 2), xaxt="n", xlab="", ylab="Effect on Gender Gap", main="(B) Gender Gap in Recruitment", cex.main=.9, col=c("black", "gray50"))
axis(1, 1:2, labels = c("High\n", "Low\n"), tick = F)
axis(1, 1.5, labels = "\nRecruitment of Women in Previous Terms", tick = F)
abline(h=0, lty=3)
points(places, gap.highlowmembership[which, 1], pch=c(16, 21), lwd=2, cex=1.3, bg=c("black", "white"), col=c("black", "gray50"))
legend("topright", col=c("black", "gray50"),  pch=c(16, 21), pt.bg=c("black", "white"), c("Female Mayors", "Male Mayors"), lwd=2, bty="n")

dev.off()







### The following lines of code format the RDD results into tables, which are reported in the Appendix


figure.3and4 <- rbind(results.formater(mod.recruitment.women,  "Recruitment Rate, Female Mayors"),
                   results.formater(mod.recruitment.men, "Recruitment Rate, Male Mayors"),
                   results.formater(mod.imbalance.women,  "Gender Gap, Female Mayors"),
                   results.formater(mod.imbalance.men,  "Gender Gap, Male Mayors"))
stargazer(as.matrix(figure.3and4), covariate.labels = c("Model", "Estimate", "95\\% CI", "p", "h", "n"), 
          title="The Effect of Female Mayors on Membership Recruitment (Figures 3 and 4)",
          out="../3_output/appendix/tableC1.tex")



figure.5 <- rbind(results.formater(mod.attrition.female,  "All Members, Female Mayors"),
                  results.formater(mod.attrition.male, "All Members, Male Mayors"),
                  results.formater(mod.attrition.women.female, "Female Members, Female Mayors"),
                  results.formater(mod.attrition.women.male, "Female Members, Male Mayors"),
                  results.formater(mod.attrition.men.female,  "Male Members, Female Mayors"),
                  results.formater(mod.attrition.men.male, "Male Members, Male Mayors"))
stargazer(as.matrix(figure.5), covariate.labels = c("Model", "Estimate", "95\\% CI", "p", "h", "n"), 
          title="The Effect of Mayors on Membership Attrition (Figure 5)",
          out="../3_output/appendix/tableC2.tex")

figure.6 <- rbind(results.formater(renominated.women,  "Renominated, Female Mayors"),
                  results.formater(renominated.men, "Renominated, Male Mayors"),
                  results.formater(advanced.women,  "Nominated to Higher, Female Mayors"),
                  results.formater(advanced.men, "Nominated to Higher, Male Mayors"))
stargazer(as.matrix(figure.6), covariate.labels = c("Model", "Estimate", "95\\% CI", "p", "h", "n"), 
          title="The Effect of the Election Outcome on the Candidate’s Career Trajectory (Figure 6)",
          out="../3_output/appendix/tableC3.tex")

figure.7a <- rbind(results.formater(advanced.women.highfemalemembership,  "Female Mayors, High Female Membership"),
                  results.formater(advanced.women.lowfemalemembership, "Female Mayors, Low Female Membership"),
                  results.formater(advanced.men.highfemalemembership,  "Male Mayors, High Female Membership"),
                  results.formater(advanced.men.lowfemalemembership, "Male Mayors, Low Female Membership"))
stargazer(as.matrix(figure.7a), covariate.labels = c("Model", "Estimate", "95\\% CI", "p", "h", "n"), 
          title="The Effect of the Election Outcome on Nomination for Higher Office, by Women’s Party Membership in Previous Terms (Figure 7, Panel A)",
          out="../3_output/appendix/tableC4.tex")

figure7b <- rbind(results.formater(gender.imbalance.FEMALEmayor.HIGHpastrecruitment,  "Female Mayors, High Female Membership"),
                  results.formater(gender.imbalance.FEMALEmayor.LOWpastrecruitment, "Female Mayors, Low Female Membership"),
                  results.formater(gender.imbalance.MALEmayor.HIGHpastrecruitment,  "Male Mayors, High Female Membership"),
                  results.formater(gender.imbalance.MALEmayor.LOWpastrecruitment, "Male Mayors, Low Female Membership"))
stargazer(as.matrix(figure7b), covariate.labels = c("Model", "Estimate", "95\\% CI", "p", "h", "n"), 
          title="The Effect of the Election Outcome on the Gender Gap in Membership Recruitment, by Women’s Party Membership in Previous Terms (Figure 7, Panel B)",
          out="../3_output/appendix/tableC5.tex")



cat("Main analyses complete.\n")




